/**
 * Created by ChenYu on 2017-07-14.
 */
var conn = new WebIM.connection({
    https: WebIM.config.https,
    url: WebIM.config.xmppURL,
    isAutoLogin: WebIM.config.isAutoLogin,
    isMultiLoginSessions: WebIM.config.isMultiLoginSessions
});
var rtcCall = new WebIM.WebRTC.Call({
    connection: conn,
    mediaStreamConstaints: {
        audio: true,
        video: true
    },

    listener: {
        onAcceptCall: function (from, options) {
            console.log('onAcceptCall::', 'from: ', from, 'options: ', options);
        },
        //通过streamType区分视频流和音频流，streamType: 'VOICE'(音频流)，'VIDEO'(视频流)
        onGotRemoteStream: function (stream, streamType) {
            console.log('onGotRemoteStream::', 'stream: ', stream, 'streamType: ', streamType);
            var video = document.getElementById('video');
            video.srcObject = stream;
        },
        onGotLocalStream: function (stream, streamType) {
            console.log('onGotLocalStream::', 'stream:', stream, 'streamType: ', streamType);
            var video = document.getElementById('localVideo');
            video.srcObject = stream;
        },
        onRinging: function (caller) {
            console.log('onRinging::', 'caller:', caller);
        },
        onTermCall: function (reason) {
            console.log('onTermCall::');
            console.log('reason:', reason);
        },
        onIceConnectionStateChange: function (iceState) {
            console.log('onIceConnectionStateChange::', 'iceState:', iceState);
        },
        onError: function (e) {
            console.log(e);
        }
    }
});

// 视频呼叫对方
var videoCall = function () {
    rtcCall.caller = '000';
    rtcCall.makeVideoCall('001');
};
// 音频呼叫对方
var voiceCall = function () {
    rtcCall.caller = '000';
    rtcCall.makeVoiceCall('001');
};
// 关掉/拒绝视频
var endCall = function () {
    rtcCall.endCall();
}
// 接受对方呼叫
var acceptCall = function () {
    rtcCall.acceptCall();
}
document.getElementById('videoCall').onclick = videoCall;
document.getElementById('voiceCall').onclick = voiceCall;

conn.listen({
    onOpened: function (message) {
        //连接成功回调
        // 如果isAutoLogin设置为false，那么必须手动设置上线，否则无法收消息
        // 手动上线指的是调用conn.setPresence(); 如果conn初始化时已将isAutoLogin设置为true
        // 则无需调用conn.setPresence();
    },
    onClosed: function (message) {
    },         //连接关闭回调
    onTextMessage: function (message) {
        console.log("收到文本消息" + message.data);
        var msg = '<h2 class="msg">' + message.data + '</h2>';
        $(".msgBox").append(msg);
    },    //收到文本消息
    onEmojiMessage: function (message) {
    },   //收到表情消息
    onPictureMessage: function (message) {
        console.log(message);
        var msg = '<img class="msg" src="' + message.url + '"/>';
        $(".msgBox").append(msg);
    }, //收到图片消息
    onCmdMessage: function (message) {
    },     //收到命令消息
    onAudioMessage: function (message) {
    },   //收到音频消息
    onLocationMessage: function (message) {
    },//收到位置消息
    onFileMessage: function (message) {
    },    //收到文件消息
    onVideoMessage: function (message) {
        var node = document.getElementById('privateVideo');
        var option = {
            url: message.url,
            headers: {
                'Accept': 'audio/mp4'
            },
            onFileDownloadComplete: function (response) {
                var objectURL = WebIM.utils.parseDownloadResponse.call(conn, response);
                node.src = objectURL;
            },
            onFileDownloadError: function () {
                console.log('File down load error.')
            }
        };
        WebIM.utils.download.call(conn, option);
    },   //收到视频消息
    onPresence: function (message) {
    },       //收到联系人订阅请求、处理群组、聊天室被踢解散等消息
    onRoster: function (message) {
    },         //处理好友申请
    onInviteMessage: function (message) {
    },  //处理群组邀请
    onOnline: function () {
    },                  //本机网络连接成功
    onOffline: function () {
    },                 //本机网络掉线
    onError: function (message) {
    },          //失败回调
    onBlacklistUpdate: function (list) {       //黑名单变动
                                               // 查询黑名单，将好友拉黑，将好友从黑名单移除都会回调这个函数，list则是黑名单现有的所有好友信息
    }
});
var options = {
    apiUrl: WebIM.config.apiURL,
    user: '000',
    pwd: 'abc000',
    appKey: WebIM.config.appkey,
    success: function () {
        console.log("登录成功");
    }
};
conn.open(options);